package com.stylefeng.guns.modular.system.service.impl;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.CurFengkouMapper;
import com.stylefeng.guns.modular.system.dao.GpcMapper;
import com.stylefeng.guns.modular.system.dao.MyGpcMapper;
import com.stylefeng.guns.modular.system.model.CurFengKData;
import com.stylefeng.guns.modular.system.model.CurFengkou;
import com.stylefeng.guns.modular.system.model.Gpc;
import com.stylefeng.guns.modular.system.model.KpFengKouData;
import com.stylefeng.guns.modular.system.model.MyGpc;
import com.stylefeng.guns.modular.system.service.IGpcService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-06-24
 */
@Service
public class GpcServiceImpl extends ServiceImpl<GpcMapper, Gpc> implements IGpcService {
	
	@Autowired
	private MyGpcMapper myGpcMapper;
	
	@Autowired
	private GpcMapper gpcMapper;
	
	@Autowired
	private CurFengkouMapper curFengkouMapper;
	
	@Override
	public List<Map<String, Object>> selectMapsByDm(Page<Gpc> page,
			String date, String dm) {
		ShiroUser shiroUser = ShiroKit.getUser();
		return gpcMapper.selectMapsByDm(page,date,dm,shiroUser.getId());
	}

	@Override
	public List<Map<String, Object>> selectByCurrentDayGpc(List<String> gScode,
			String gTime,String buyDate,Integer zlje,Integer mp) {
		return gpcMapper.selectByCurrentDayGpc(gScode,gTime,buyDate,zlje,mp);
	}
	
	

	@Override
	public void updatefk(String time,String token, String userid) {
		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("a", KpMethod.A.getMethod());
		params.put("c", "LongHuBang");
		params.put("Index", "0");
		params.put("st", "300");
		params.put("Time", time);
		params.put("Token", token);
		params.put("UserID", userid);
		String result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), params,false);
		if(StringUtils.isNotEmpty(result)){
			List<KpFengKouData> fengKouDatas = JSON.parseArray(JSON.parseObject(result).getString("list"),KpFengKouData.class);
			for(KpFengKouData kpFengKouData : fengKouDatas){
				Gpc gpc = new Gpc();
				gpc.setgTdate(DateUtil.parseDate(time));
				gpc.setgScode(kpFengKouData.getID());
				gpc = gpcMapper.selectByDate(gpc);
				if(gpc != null){
					gpc.setgReason(kpFengKouData.getFengKou());
					gpcMapper.updateById(gpc);
				}
				Wrapper<MyGpc> wrapper = new EntityWrapper<MyGpc>();
				wrapper.eq("g_scode", kpFengKouData.getID());
				List<MyGpc> myGpcs = myGpcMapper.selectList(wrapper);
				if(CollectionUtils.isNotEmpty(myGpcs)){
					for(MyGpc myGpc : myGpcs){
						myGpc.setgReason(kpFengKouData.getFengKou());
						myGpcMapper.updateById(myGpc);
					}
				}
			}
		}
	}

	@Override
	public void updateCurfk(String time, String token, String userid) {
		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("a", KpMethod.D.getMethod());
		params.put("c", "StockFengKData");
		params.put("Day", "");
		params.put("Index", "0");
		params.put("Order", "12");
		params.put("st", "300");
		params.put("Time", DateUtil.getMMSSTime());
		params.put("Token", token);
		params.put("UserID", userid);
		String result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), params,true);
		if(StringUtils.isNotEmpty(result)){
			List<CurFengKData> fengKouDatas = JSON.parseArray(JSON.parseObject(result).getString("List"),CurFengKData.class);
			for(CurFengKData kpFengKouData : fengKouDatas){
				CurFengkou curFengkou = new CurFengkou();
				curFengkou.setgDm(kpFengKouData.getStockID());
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(Long.parseLong(kpFengKouData.getTime() + StringUtils.substring(kpFengKouData.getCirVal(), kpFengKouData.getCirVal().length() - 3, kpFengKouData.getCirVal().length())));
				curFengkou.setgDate(cal.getTime());
				curFengkou = curFengkouMapper.selectByDate(curFengkou);
				if(curFengkou == null){
					curFengkou = new CurFengkou();
					curFengkou.setgDate(cal.getTime());
					curFengkou.setgDm(kpFengKouData.getStockID());
					curFengkou.setgName(kpFengKouData.getStockName());
					curFengkou.setgRate(kpFengKouData.getRate());
					curFengkou.setgTrade(kpFengKouData.getTrade());
					curFengkou.setgZlbuy(kpFengKouData.getZLBuy());
					curFengkou.setgZlje(kpFengKouData.getZLJE());
					curFengkou.setgZlshell(kpFengKouData.getZLSell());
					curFengkou.setgPrice(kpFengKouData.getPrice());
					curFengkouMapper.insert(curFengkou);
				}else{
					if(kpFengKouData.getZLBuy() ==0l
							||kpFengKouData.getZLJE() ==0l
							||kpFengKouData.getZLSell() ==0l){
						continue;
					}
					curFengkou.setgRate(kpFengKouData.getRate());
					curFengkou.setgTrade(kpFengKouData.getTrade());
					curFengkou.setgZlbuy(kpFengKouData.getZLBuy());
					curFengkou.setgZlje(kpFengKouData.getZLJE());
					curFengkou.setgZlshell(kpFengKouData.getZLSell());
					curFengkou.setgPrice(kpFengKouData.getPrice());
					curFengkouMapper.updateById(curFengkou);
				}
			}
		}
	}

	@Override
	public List<Map<String, Object>> selectMapsByGn(Page<Gpc> page, String gn,String kpd) {
		return gpcMapper.selectMapsByGn(page,gn,kpd);
	}
}
